<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta
            http-equiv="Content-Type"
            content="text/html; charset=utf-8"
        />
        <meta name="viewport" content="width=device-width, initial-scale=1" />

        <title>Socket Mode Client &mdash; Python Slack SDK</title>

        <!-- Google Tag Manager -->
        <script>
            (function(w, d, s, l, i) {
                w[l] = w[l] || [];
                w[l].push({
                    'gtm.start': new Date().getTime(),
                    event: 'gtm.js'
                });
                var f = d.getElementsByTagName(s)[0],
                    j = d.createElement(s),
                    dl = l != 'dataLayer' ? '&l=' + l : '';
                j.async = true;
                j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
                f.parentNode.insertBefore(j, f);
            })(window, document, 'script', 'dataLayer', 'GTM-KFZ5MK7');
        </script>
        <!-- End Google Tag Manager -->
        
        <link
            href="https://a.slack-edge.com/4f227/style/rollup-slack_kit_legacy_adapters.css"
            rel="stylesheet"
            type="text/css"
        />
        <link
            href="https://a.slack-edge.com/3e02c0/style/rollup-api_site.css"
            rel="stylesheet"
            type="text/css"
        />
        <link
            rel="stylesheet"
            href=".././assets/default.css"
            type="text/css"
        />
        <link
            rel="stylesheet"
            href=".././assets/pygments.css"
            type="text/css"
        />
        <link
            rel="stylesheet"
            href=".././assets/docs.css"
            type="text/css"
        />
        <link
            id="favicon"
            rel="shortcut icon"
            href="https://a.slack-edge.com/4f28/img/icons/favicon-32.png"
            type="image/png"
        />
        <link
            rel="top"
            title="Python Slack SDK 1.0.1 documentation"
            href="../index.html"
        />
    </head>

    <body class="api light_theme">
        <!-- Google Tag Manager (noscript) -->
        <noscript
            ><iframe
                src="https://www.googletagmanager.com/ns.html?id=GTM-KFZ5MK7"
                height="0"
                width="0"
                style="display:none;visibility:hidden"
            ></iframe
        ></noscript>
        <!-- End Google Tag Manager (noscript) -->
        <header>
            <a id="menu_toggle" class="no_transition show_on_mobile">
                <span class="menu_icon"></span>
                <span class="vert_divider"></span>
            </a>
            <a
                href="https://api.slack.com/"
                id="header_logo"
                class="api hide_on_mobile"
                style="float:left; display: inline-block;"
            >
                <img
                    alt="Slack API"
                    src="https://a.slack-edge.com/3026cb/img/slack_api_logo_vogue.png"
                    style="width: 225px; padding-right: 25px; border-right: 1px solid #DDD;"
                />
            </a>
            <span
                style="display: inline-block; padding-left: 20px; margin-top: 25px; font-weight: bold;"
            >
                <a style="color: #555459;" href="./index.html">Python Slack SDK</a>
            </span>
            <div class="header_nav">
                <a
                    href="https://github.com/SlackAPI/python-slack-sdk"
                    class="btn header_btn float_right"
                    data-qa="go_to_slack"
                    >Go to GitHub</a
                >
            </div>
        </header>
        

        <div id="page">
            <div id="page_contents" class="clearfix">
                <!-- Sidebar Content -->
                <nav id="api_nav" class="col span_1_of_4">
                    <div id="api_sections">
                         <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../index.html">Python Slack SDK</a></li>
<li class="toctree-l1"><a class="reference internal" href="../v3-migration/index.html">Migration Guide</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../v3-migration/index.html#from-slackclient-2-x">From slackclient 2.x</a></li>
<li class="toctree-l2"><a class="reference internal" href="../v3-migration/index.html#from-slackclient-1-x">From slackclient 1.x</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../installation/index.html#access-tokens">Access Tokens</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/index.html#workspace-installations">Workspace Installations</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../web/index.html">Web Client</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../web/index.html#messaging">Messaging</a></li>
<li class="toctree-l2"><a class="reference internal" href="../web/index.html#files">Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="../web/index.html#conversations">Conversations</a></li>
<li class="toctree-l2"><a class="reference internal" href="../web/index.html#modals">Modals</a></li>
<li class="toctree-l2"><a class="reference internal" href="../web/index.html#rate-limits">Rate Limits</a><ul>
<li class="toctree-l3"><a class="reference internal" href="../web/index.html#calling-any-api-methods">Calling any API methods</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../web/index.html#asyncwebclient">AsyncWebClient</a></li>
<li class="toctree-l2"><a class="reference internal" href="../web/index.html#retryhandler">RetryHandler</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../webhook/index.html">Webhook Client</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../webhook/index.html#id1">Incoming Webhooks</a></li>
<li class="toctree-l2"><a class="reference internal" href="../webhook/index.html#response-url">response_url</a></li>
<li class="toctree-l2"><a class="reference internal" href="../webhook/index.html#asyncwebhookclient">AsyncWebhookClient</a></li>
<li class="toctree-l2"><a class="reference internal" href="../webhook/index.html#retryhandler">RetryHandler</a></li>
</ul>
</li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Socket Mode Client</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#socketmodeclient">SocketModeClient</a></li>
<li class="toctree-l2"><a class="reference internal" href="#supported-libraries">Supported Libraries</a></li>
<li class="toctree-l2"><a class="reference internal" href="#asyncio-based-libraries">Asyncio Based Libraries</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../oauth/index.html">OAuth Modules</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../oauth/index.html#app-installation-flow">App Installation Flow</a></li>
<li class="toctree-l2"><a class="reference internal" href="../oauth/index.html#token-lookup">Token Lookup</a></li>
<li class="toctree-l2"><a class="reference internal" href="../oauth/index.html#sign-in-with-slack">Sign in with Slack</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../audit-logs/index.html">Audit Logs API Client</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../audit-logs/index.html#auditlogsclient">AuditLogsClient</a></li>
<li class="toctree-l2"><a class="reference internal" href="../audit-logs/index.html#asyncauditlogsclient">AsyncAuditLogsClient</a></li>
<li class="toctree-l2"><a class="reference internal" href="../audit-logs/index.html#retryhandler">RetryHandler</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../scim/index.html">SCIM API Client</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../scim/index.html#scimclient">SCIMClient</a></li>
<li class="toctree-l2"><a class="reference internal" href="../scim/index.html#asyncscimclient">AsyncSCIMClient</a></li>
<li class="toctree-l2"><a class="reference internal" href="../scim/index.html#retryhandler">RetryHandler</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../real_time_messaging.html">RTM Client</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../real_time_messaging.html#real-time-messaging-rtm">Real Time Messaging (RTM)</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../faq.html">FAQ</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../faq.html#python-documents">Python Documents</a></li>
<li class="toctree-l2"><a class="reference internal" href="../faq.html#installation-issues">Installation Issues</a></li>
<li class="toctree-l2"><a class="reference internal" href="../faq.html#bug-report">Bug Report</a></li>
<li class="toctree-l2"><a class="reference internal" href="../faq.html#feature-requests">Feature Requests</a></li>
<li class="toctree-l2"><a class="reference internal" href="../faq.html#contributions">Contributions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../faq.html#documentation">Documentation</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../about.html">About</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../about.html#product-name">Python Slack SDK</a></li>
</ul>
</li>
</ul>


<div id="footer">

        <ul id="footer_nav">
                <li><a href="https://github.com/SlackAPI/python-slack-sdk/blob/main/LICENSE">License</a></li>
                <li><a href="https://slackhq.github.io/code-of-conduct">Code of Conduct</a></li>
                <li><a href="https://github.com/slackapi/python-slack-sdk/blob/main/.github/contributing.md">Contributing</a></li>
                <li><a href="https://docs.google.com/a/slack-corp.com/forms/d/e/1FAIpQLSfzjVoCM7ohBnjWf7eDYQxzti1EPpinsIJQA5RAUBwJKRUQHg/viewform">Contributor License Agreement</a></li>
        </ul>

        <p id="footer_signature">Made with <i class="ts_icon ts_icon_heart"></i> by Slack<br/>and our Lovely
                Community
        </p>
</div> 
                    </div>
                </nav>
                <!-- /Sidebar Content -->

                <!-- Body Content -->
                <div class="col span_3_of_4">
                    <!-- <div class="section-title">Socket Mode Client</div> -->
                    <div class="card">
  <section id="socket-mode-client">
<h1>Socket Mode Client<a class="headerlink" href="#socket-mode-client" title="Link to this heading">¶</a></h1>
<p>Socket Mode is a method of connecting your app to Slack’s APIs using WebSockets instead of HTTP. You can use  <code class="docutils literal notranslate"><span class="pre">slack_sdk.socket_mode.SocketModeClient</span></code> for managing <a class="reference external" href="https://api.slack.com/apis/connections/socket">Socket Mode</a> connections and performing interactions with Slack.</p>
<p>The Python document for this module is available at <a class="reference external" href="https://slack.dev/python-slack-sdk/api-docs/slack_sdk/">https://slack.dev/python-slack-sdk/api-docs/slack_sdk/</a></p>
<section id="socketmodeclient">
<h2>SocketModeClient<a class="headerlink" href="#socketmodeclient" title="Link to this heading">¶</a></h2>
<p>First off, let’s start with enabling Socket Mode. Visit <a class="reference external" href="http://api.slack.com/apps">the Slack App configuration page</a>, choose the app you’re working on, and go to <strong>Settings</strong> on the left pane. There are a few things to do on the page.</p>
<ul>
<li><p>Go to <strong>Settings</strong> &gt; <strong>Basic Information</strong>, then add a new <strong>App-Level Token</strong> with the <cite>connections:write</cite> scope</p></li>
<li><p>Go to <strong>Settings</strong> &gt; <strong>Socket Mode</strong>, then turn on <strong>Enable Socket Mode</strong></p></li>
<li><p>Go to <strong>Features</strong> &gt; <strong>App Home</strong>, look under <strong>Show Tabs</strong> &gt; <strong>Messages Tab</strong> then turn on <strong>Allow users to send Slash commands and messages from the messages tab</strong></p></li>
<li><p>Go to <strong>Features</strong> &gt; <strong>Event Subscriptions</strong>, then turn on <strong>Enable Events</strong></p>
<blockquote>
<div><ul>
<li><p>On the same page expand <strong>Subscribe to bot events</strong> click <strong>Add Bot User Event</strong> and select <strong>message.im</strong></p>
<blockquote>
<div><ul class="simple">
<li><p>This will allow the bot to get events for messages that are sent in 1:1 direct messages with itself</p></li>
</ul>
</div></blockquote>
</li>
</ul>
</div></blockquote>
</li>
<li><p>Go to <strong>Features</strong> &gt; <strong>Interactivity and Shortcuts</strong>, look under <em>Shortcuts*</em> click <strong>Create a New Shortcut</strong> then create a new Global shortcut with the following details</p>
<blockquote>
<div><ul class="simple">
<li><p><strong>Name</strong>: Hello</p></li>
<li><p><strong>Short Description</strong>: Receive a Greeting</p></li>
<li><p><strong>Callback ID</strong>: hello-shortcut</p></li>
</ul>
</div></blockquote>
</li>
<li><p>Go to <strong>Features</strong> &gt; <strong>OAuth &amp; Permissions</strong> under <strong>Scopes</strong> &gt; <strong>Bot Token Scopes</strong> click <strong>Add an OAuth Scope</strong> and select <strong>reactions:write</strong></p>
<blockquote>
<div><ul class="simple">
<li><p>This will allow the bot to add emoji reactions (Reacji’s) to messages</p></li>
</ul>
</div></blockquote>
</li>
<li><p>Go to <strong>Features</strong> &gt; <strong>Oauth &amp; Permissions</strong> under <strong>OAuth Tokens for Your Workspace</strong> click <strong>Install to Workspace</strong></p></li>
</ul>
<p>You will be using the app-level token that starts with <code class="docutils literal notranslate"><span class="pre">xapp-</span></code> prefix. Note that the token here is not the ones starting with either <code class="docutils literal notranslate"><span class="pre">xoxb-</span></code> or <code class="docutils literal notranslate"><span class="pre">xoxp-</span></code>.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">slack_sdk.web</span> <span class="kn">import</span> <span class="n">WebClient</span>
<span class="kn">from</span> <span class="nn">slack_sdk.socket_mode</span> <span class="kn">import</span> <span class="n">SocketModeClient</span>

<span class="c1"># Initialize SocketModeClient with an app-level token + WebClient</span>
<span class="n">client</span> <span class="o">=</span> <span class="n">SocketModeClient</span><span class="p">(</span>
    <span class="c1"># This app-level token will be used only for establishing a connection</span>
    <span class="n">app_token</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;SLACK_APP_TOKEN&quot;</span><span class="p">),</span>  <span class="c1"># xapp-A111-222-xyz</span>
    <span class="c1"># You will be using this WebClient for performing Web API calls in listeners</span>
    <span class="n">web_client</span><span class="o">=</span><span class="n">WebClient</span><span class="p">(</span><span class="n">token</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;SLACK_BOT_TOKEN&quot;</span><span class="p">))</span>  <span class="c1"># xoxb-111-222-xyz</span>
<span class="p">)</span>

<span class="kn">from</span> <span class="nn">slack_sdk.socket_mode.response</span> <span class="kn">import</span> <span class="n">SocketModeResponse</span>
<span class="kn">from</span> <span class="nn">slack_sdk.socket_mode.request</span> <span class="kn">import</span> <span class="n">SocketModeRequest</span>

<span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">client</span><span class="p">:</span> <span class="n">SocketModeClient</span><span class="p">,</span> <span class="n">req</span><span class="p">:</span> <span class="n">SocketModeRequest</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">req</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="s2">&quot;events_api&quot;</span><span class="p">:</span>
        <span class="c1"># Acknowledge the request anyway</span>
        <span class="n">response</span> <span class="o">=</span> <span class="n">SocketModeResponse</span><span class="p">(</span><span class="n">envelope_id</span><span class="o">=</span><span class="n">req</span><span class="o">.</span><span class="n">envelope_id</span><span class="p">)</span>
        <span class="n">client</span><span class="o">.</span><span class="n">send_socket_mode_response</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>

        <span class="c1"># Add a reaction to the message if it&#39;s a new message</span>
        <span class="k">if</span> <span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="p">[</span><span class="s2">&quot;event&quot;</span><span class="p">][</span><span class="s2">&quot;type&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;message&quot;</span> \
            <span class="ow">and</span> <span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="p">[</span><span class="s2">&quot;event&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;subtype&quot;</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">client</span><span class="o">.</span><span class="n">web_client</span><span class="o">.</span><span class="n">reactions_add</span><span class="p">(</span>
                <span class="n">name</span><span class="o">=</span><span class="s2">&quot;eyes&quot;</span><span class="p">,</span>
                <span class="n">channel</span><span class="o">=</span><span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="p">[</span><span class="s2">&quot;event&quot;</span><span class="p">][</span><span class="s2">&quot;channel&quot;</span><span class="p">],</span>
                <span class="n">timestamp</span><span class="o">=</span><span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="p">[</span><span class="s2">&quot;event&quot;</span><span class="p">][</span><span class="s2">&quot;ts&quot;</span><span class="p">],</span>
            <span class="p">)</span>
    <span class="k">if</span> <span class="n">req</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="s2">&quot;interactive&quot;</span> \
        <span class="ow">and</span> <span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;type&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="s2">&quot;shortcut&quot;</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="p">[</span><span class="s2">&quot;callback_id&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;hello-shortcut&quot;</span><span class="p">:</span>
            <span class="c1"># Acknowledge the request</span>
            <span class="n">response</span> <span class="o">=</span> <span class="n">SocketModeResponse</span><span class="p">(</span><span class="n">envelope_id</span><span class="o">=</span><span class="n">req</span><span class="o">.</span><span class="n">envelope_id</span><span class="p">)</span>
            <span class="n">client</span><span class="o">.</span><span class="n">send_socket_mode_response</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
            <span class="c1"># Open a welcome modal</span>
            <span class="n">client</span><span class="o">.</span><span class="n">web_client</span><span class="o">.</span><span class="n">views_open</span><span class="p">(</span>
                <span class="n">trigger_id</span><span class="o">=</span><span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="p">[</span><span class="s2">&quot;trigger_id&quot;</span><span class="p">],</span>
                <span class="n">view</span><span class="o">=</span><span class="p">{</span>
                    <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;modal&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;callback_id&quot;</span><span class="p">:</span> <span class="s2">&quot;hello-modal&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;title&quot;</span><span class="p">:</span> <span class="p">{</span>
                        <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;plain_text&quot;</span><span class="p">,</span>
                        <span class="s2">&quot;text&quot;</span><span class="p">:</span> <span class="s2">&quot;Greetings!&quot;</span>
                    <span class="p">},</span>
                    <span class="s2">&quot;submit&quot;</span><span class="p">:</span> <span class="p">{</span>
                        <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;plain_text&quot;</span><span class="p">,</span>
                        <span class="s2">&quot;text&quot;</span><span class="p">:</span> <span class="s2">&quot;Good Bye&quot;</span>
                    <span class="p">},</span>
                    <span class="s2">&quot;blocks&quot;</span><span class="p">:</span> <span class="p">[</span>
                        <span class="p">{</span>
                            <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;section&quot;</span><span class="p">,</span>
                            <span class="s2">&quot;text&quot;</span><span class="p">:</span> <span class="p">{</span>
                                <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;mrkdwn&quot;</span><span class="p">,</span>
                                <span class="s2">&quot;text&quot;</span><span class="p">:</span> <span class="s2">&quot;Hello!&quot;</span>
                            <span class="p">}</span>
                        <span class="p">}</span>
                    <span class="p">]</span>
                <span class="p">}</span>
            <span class="p">)</span>

    <span class="k">if</span> <span class="n">req</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="s2">&quot;interactive&quot;</span> \
        <span class="ow">and</span> <span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;type&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="s2">&quot;view_submission&quot;</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="p">[</span><span class="s2">&quot;view&quot;</span><span class="p">][</span><span class="s2">&quot;callback_id&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;hello-modal&quot;</span><span class="p">:</span>
            <span class="c1"># Acknowledge the request and close the modal</span>
            <span class="n">response</span> <span class="o">=</span> <span class="n">SocketModeResponse</span><span class="p">(</span><span class="n">envelope_id</span><span class="o">=</span><span class="n">req</span><span class="o">.</span><span class="n">envelope_id</span><span class="p">)</span>
            <span class="n">client</span><span class="o">.</span><span class="n">send_socket_mode_response</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>

<span class="c1"># Add a new listener to receive messages from Slack</span>
<span class="c1"># You can add more listeners like this</span>
<span class="n">client</span><span class="o">.</span><span class="n">socket_mode_request_listeners</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">process</span><span class="p">)</span>
<span class="c1"># Establish a WebSocket connection to the Socket Mode servers</span>
<span class="n">client</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
<span class="c1"># Just not to stop this process</span>
<span class="kn">from</span> <span class="nn">threading</span> <span class="kn">import</span> <span class="n">Event</span>
<span class="n">Event</span><span class="p">()</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
</pre></div>
</div>
</section>
<hr class="docutils" />
<section id="supported-libraries">
<h2>Supported Libraries<a class="headerlink" href="#supported-libraries" title="Link to this heading">¶</a></h2>
<p>This SDK offers its own simple WebSocket client covering only required features for Socket Mode. In addition to that, <code class="docutils literal notranslate"><span class="pre">SocketModeClient</span></code> is implemented with a few 3rd party open-source libraries. If you prefer any of the following, you can use it over the built-in one.</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>PyPI Project</p></th>
<th class="head"><p>SocketModeClient</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><a class="reference external" href="https://pypi.org/project/slack-sdk/">slack_sdk</a></p></td>
<td><p><a class="reference external" href="https://github.com/slackapi/python-slack-sdk/blob/main/slack_sdk/socket_mode/builtin">slack_sdk.socket_mode.SocketModeClient</a></p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference external" href="https://pypi.org/project/websocket_client/">websocket_client</a></p></td>
<td><p><a class="reference external" href="https://github.com/slackapi/python-slack-sdk/blob/main/slack_sdk/socket_mode/websocket_client">slack_sdk.socket_mode.websocket_client.SocketModeClient</a></p></td>
</tr>
<tr class="row-even"><td><p><a class="reference external" href="https://pypi.org/project/aiohttp/">aiohttp</a> (asyncio-based)</p></td>
<td><p><a class="reference external" href="https://github.com/slackapi/python-slack-sdk/blob/main/slack_sdk/socket_mode/aiohttp">slack_sdk.socket_mode.aiohttp.SocketModeClient</a></p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference external" href="https://pypi.org/project/websockets/">websockets</a> (asyncio-based)</p></td>
<td><p><a class="reference external" href="https://github.com/slackapi/python-slack-sdk/blob/main/slack_sdk/socket_mode/websockets">slack_sdk.socket_mode.websockets.SocketModeClient</a></p></td>
</tr>
</tbody>
</table>
<p>To use the <a class="reference external" href="https://pypi.org/project/websocket_client/">websocket_client</a> based one, all you need to do are to add <a class="reference external" href="https://pypi.org/project/websocket_client/">websocket_client</a> dependency and to change the import as below.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Note that the pockage is different</span>
<span class="kn">from</span> <span class="nn">slack_sdk.socket_mode.websocket_client</span> <span class="kn">import</span> <span class="n">SocketModeClient</span>

<span class="n">client</span> <span class="o">=</span> <span class="n">SocketModeClient</span><span class="p">(</span>
    <span class="n">app_token</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;SLACK_APP_TOKEN&quot;</span><span class="p">),</span>  <span class="c1"># xapp-A111-222-xyz</span>
    <span class="n">web_client</span><span class="o">=</span><span class="n">WebClient</span><span class="p">(</span><span class="n">token</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;SLACK_BOT_TOKEN&quot;</span><span class="p">))</span>  <span class="c1"># xoxb-111-222-xyz</span>
<span class="p">)</span>
</pre></div>
</div>
<p>You can pass a few additional arguments that are specific to the library. Apart from that, all the functionalities work in the same way with the built-in version.</p>
</section>
<hr class="docutils" />
<section id="asyncio-based-libraries">
<h2>Asyncio Based Libraries<a class="headerlink" href="#asyncio-based-libraries" title="Link to this heading">¶</a></h2>
<p>To use the asyncio-based ones such as aiohttp, your app needs to be compatible with asyncio’s async/await programming model. The <cite>SocketModeClient</cite> only works with <cite>AsyncWebClient</cite> and async listeners.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">slack_sdk.web.async_client</span> <span class="kn">import</span> <span class="n">AsyncWebClient</span>
<span class="kn">from</span> <span class="nn">slack_sdk.socket_mode.aiohttp</span> <span class="kn">import</span> <span class="n">SocketModeClient</span>

<span class="c1"># Use async method</span>
<span class="k">async</span> <span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
    <span class="kn">from</span> <span class="nn">slack_sdk.socket_mode.response</span> <span class="kn">import</span> <span class="n">SocketModeResponse</span>
    <span class="kn">from</span> <span class="nn">slack_sdk.socket_mode.request</span> <span class="kn">import</span> <span class="n">SocketModeRequest</span>

    <span class="c1"># Initialize SocketModeClient with an app-level token + AsyncWebClient</span>
    <span class="n">client</span> <span class="o">=</span> <span class="n">SocketModeClient</span><span class="p">(</span>
        <span class="c1"># This app-level token will be used only for establishing a connection</span>
        <span class="n">app_token</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;SLACK_APP_TOKEN&quot;</span><span class="p">),</span>  <span class="c1"># xapp-A111-222-xyz</span>
        <span class="c1"># You will be using this AsyncWebClient for performing Web API calls in listeners</span>
        <span class="n">web_client</span><span class="o">=</span><span class="n">AsyncWebClient</span><span class="p">(</span><span class="n">token</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;SLACK_BOT_TOKEN&quot;</span><span class="p">))</span>  <span class="c1"># xoxb-111-222-xyz</span>
    <span class="p">)</span>

    <span class="c1"># Use async method</span>
    <span class="k">async</span> <span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">client</span><span class="p">:</span> <span class="n">SocketModeClient</span><span class="p">,</span> <span class="n">req</span><span class="p">:</span> <span class="n">SocketModeRequest</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">req</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="s2">&quot;events_api&quot;</span><span class="p">:</span>
            <span class="c1"># Acknowledge the request anyway</span>
            <span class="n">response</span> <span class="o">=</span> <span class="n">SocketModeResponse</span><span class="p">(</span><span class="n">envelope_id</span><span class="o">=</span><span class="n">req</span><span class="o">.</span><span class="n">envelope_id</span><span class="p">)</span>
            <span class="c1"># Don&#39;t forget having await for method calls</span>
            <span class="k">await</span> <span class="n">client</span><span class="o">.</span><span class="n">send_socket_mode_response</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>

            <span class="c1"># Add a reaction to the message if it&#39;s a new message</span>
            <span class="k">if</span> <span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="p">[</span><span class="s2">&quot;event&quot;</span><span class="p">][</span><span class="s2">&quot;type&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;message&quot;</span> \
                <span class="ow">and</span> <span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="p">[</span><span class="s2">&quot;event&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;subtype&quot;</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">await</span> <span class="n">client</span><span class="o">.</span><span class="n">web_client</span><span class="o">.</span><span class="n">reactions_add</span><span class="p">(</span>
                    <span class="n">name</span><span class="o">=</span><span class="s2">&quot;eyes&quot;</span><span class="p">,</span>
                    <span class="n">channel</span><span class="o">=</span><span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="p">[</span><span class="s2">&quot;event&quot;</span><span class="p">][</span><span class="s2">&quot;channel&quot;</span><span class="p">],</span>
                    <span class="n">timestamp</span><span class="o">=</span><span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="p">[</span><span class="s2">&quot;event&quot;</span><span class="p">][</span><span class="s2">&quot;ts&quot;</span><span class="p">],</span>
                <span class="p">)</span>
        <span class="k">if</span> <span class="n">req</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="s2">&quot;interactive&quot;</span> \
            <span class="ow">and</span> <span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;type&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="s2">&quot;shortcut&quot;</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="p">[</span><span class="s2">&quot;callback_id&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;hello-shortcut&quot;</span><span class="p">:</span>
                <span class="c1"># Acknowledge the request</span>
                <span class="n">response</span> <span class="o">=</span> <span class="n">SocketModeResponse</span><span class="p">(</span><span class="n">envelope_id</span><span class="o">=</span><span class="n">req</span><span class="o">.</span><span class="n">envelope_id</span><span class="p">)</span>
                <span class="k">await</span> <span class="n">client</span><span class="o">.</span><span class="n">send_socket_mode_response</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
                <span class="c1"># Open a welcome modal</span>
                <span class="k">await</span> <span class="n">client</span><span class="o">.</span><span class="n">web_client</span><span class="o">.</span><span class="n">views_open</span><span class="p">(</span>
                    <span class="n">trigger_id</span><span class="o">=</span><span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="p">[</span><span class="s2">&quot;trigger_id&quot;</span><span class="p">],</span>
                    <span class="n">view</span><span class="o">=</span><span class="p">{</span>
                        <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;modal&quot;</span><span class="p">,</span>
                        <span class="s2">&quot;callback_id&quot;</span><span class="p">:</span> <span class="s2">&quot;hello-modal&quot;</span><span class="p">,</span>
                        <span class="s2">&quot;title&quot;</span><span class="p">:</span> <span class="p">{</span>
                            <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;plain_text&quot;</span><span class="p">,</span>
                            <span class="s2">&quot;text&quot;</span><span class="p">:</span> <span class="s2">&quot;Greetings!&quot;</span>
                        <span class="p">},</span>
                        <span class="s2">&quot;submit&quot;</span><span class="p">:</span> <span class="p">{</span>
                            <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;plain_text&quot;</span><span class="p">,</span>
                            <span class="s2">&quot;text&quot;</span><span class="p">:</span> <span class="s2">&quot;Good Bye&quot;</span>
                        <span class="p">},</span>
                        <span class="s2">&quot;blocks&quot;</span><span class="p">:</span> <span class="p">[</span>
                            <span class="p">{</span>
                                <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;section&quot;</span><span class="p">,</span>
                                <span class="s2">&quot;text&quot;</span><span class="p">:</span> <span class="p">{</span>
                                    <span class="s2">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;mrkdwn&quot;</span><span class="p">,</span>
                                    <span class="s2">&quot;text&quot;</span><span class="p">:</span> <span class="s2">&quot;Hello!&quot;</span>
                                <span class="p">}</span>
                            <span class="p">}</span>
                        <span class="p">]</span>
                    <span class="p">}</span>
                <span class="p">)</span>

        <span class="k">if</span> <span class="n">req</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="s2">&quot;interactive&quot;</span> \
            <span class="ow">and</span> <span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;type&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="s2">&quot;view_submission&quot;</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">req</span><span class="o">.</span><span class="n">payload</span><span class="p">[</span><span class="s2">&quot;view&quot;</span><span class="p">][</span><span class="s2">&quot;callback_id&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;hello-modal&quot;</span><span class="p">:</span>
                <span class="c1"># Acknowledge the request and close the modal</span>
                <span class="n">response</span> <span class="o">=</span> <span class="n">SocketModeResponse</span><span class="p">(</span><span class="n">envelope_id</span><span class="o">=</span><span class="n">req</span><span class="o">.</span><span class="n">envelope_id</span><span class="p">)</span>
                <span class="k">await</span> <span class="n">client</span><span class="o">.</span><span class="n">send_socket_mode_response</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>

    <span class="c1"># Add a new listener to receive messages from Slack</span>
    <span class="c1"># You can add more listeners like this</span>
    <span class="n">client</span><span class="o">.</span><span class="n">socket_mode_request_listeners</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">process</span><span class="p">)</span>
    <span class="c1"># Establish a WebSocket connection to the Socket Mode servers</span>
    <span class="k">await</span> <span class="n">client</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
    <span class="c1"># Just not to stop this process</span>
    <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="s2">&quot;inf&quot;</span><span class="p">))</span>

<span class="c1"># You can go with other way to run it. This is just for easiness to try it out.</span>
<span class="n">asyncio</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">main</span><span class="p">())</span>
</pre></div>
</div>
</section>
</section>


                        <div class="clear_both large_bottom_margin"></div>
                    </div>
                </div>
                <!-- /Body Content -->
            </div>
        </div>

        <footer>
            <p class="light tiny align_center">
                © 2015- Slack Technologies, LLC and contributors
            </p>
        </footer>

        <script>
            window.ga =
                window.ga ||
                function() {
                    (ga.q = ga.q || []).push(arguments);
                };
            ga.l = +new Date();
            ga('create', 'UA-56978219-13', 'auto');
            ga('send', 'pageview');
        </script>
        <script async src="https://www.google-analytics.com/analytics.js"></script>
    </body>
</html>